<template>
  <div class="bg">
    <img :src="currentImage" alt="">
    
    <!-- 游戏信息栏 - 新增 -->
    <div class="game-header">
      <div class="thumbnail"><img :src="thumbnail" alt=""></div>
      <div class="tag"><img :src="level" alt=""></div>
      <div class="stats">
        <div class="stat-item">
          <span class="stat-icon">🎯</span>
          <span class="stat-value">关卡: {{ stage }}</span>
        </div>
        <div class="stat-item">
          <span class="stat-icon">⏱️</span>
          <span class="stat-value">{{ timer }}s</span>
        </div>
        <div class="stat-item">
          <span class="stat-icon">✨</span>
          <span class="stat-value">步数: {{ moves }}</span>
        </div>
      </div>
    </div>

    <!-- 拼图区域 - 保持原有结构不变 -->
    <ul class="game">
      <li v-for="(l, i) in imgArr" :key="l.index" @click="move(l.index, i, $event)" :class="lic">
        <img :src="l.img" alt="">
        <div class="mask" v-if="selCls[i]"></div>
        <!-- 新增提示标记 -->
        <div class="piece-hint" v-if="showHints && l.index === i + 1">✓</div>
      </li>
    </ul>

    <!-- 游戏控制栏 - 新增 -->
    <div class="game-controls">
      <button class="control-btn hint-btn" @click="toggleHints">
        <span class="btn-icon">💡</span>
        <span class="btn-text">{{ showHints ? '隐藏提示' : '显示提示' }}</span>
      </button>
      <button class="control-btn shuffle-btn" @click="shufflePuzzle">
        <span class="btn-icon">🔀</span>
        <span class="btn-text">重新洗牌</span>
      </button>
      <button class="control-btn music-btn" @click="toggleMusic">
        <span class="btn-icon">{{ musicPlaying ? '🔊' : '🔇' }}</span>
        <span class="btn-text">{{ musicPlaying ? '音乐开' : '音乐关' }}</span>
      </button>
    </div>

    <!-- 进度动画 - 新增 -->
    <div class="progress-bar">
      <div class="progress-fill" :style="{width: progress + '%'}"></div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'game',
  data() {
    return {
      initArr: [],
      succArr: [],
      selImg: [],
      selCls: [],
      num: 0,
      thumbnail: null,
      level: null,
      stage: 1,
      lic: 'lic',
      currentImage: '',
      // 新增数据
      moves: 0,
      timer: 0,
      timerInterval: null,
      showHints: false,
      musicPlaying: false,
      progress: 0,
      images : [
        'https://img-blog.csdnimg.cn/direct/e8959120f73247ab9652abd1cfa095b4.jpeg',
        'https://img-blog.csdnimg.cn/direct/a1aeab38a26c406cb67761d44e058272.png',
        'https://img-blog.csdnimg.cn/direct/27cc128fe4454bd386a136cc59dd830f.png',
        'https://img-blog.csdnimg.cn/direct/de782655f0004234ac7cd23992187efe.jpeg',
        'https://img-blog.csdnimg.cn/direct/d80112e050e94aadbf440a5f59ff40dc.jpeg',
        'https://img-blog.csdnimg.cn/direct/46f9547e01ff479aa1d86677108a04fb.jpeg',
        'https://img-blog.csdnimg.cn/direct/141289a0653f471ebb692cef819bfcb1.jpeg',
        'https://img-blog.csdnimg.cn/direct/7a293182c22840dfab124ea006dfbac5.png',
        'https://img-blog.csdnimg.cn/direct/5790e295b59c4645baeeb526fc4723b1.png',
        'https://img-blog.csdnimg.cn/direct/b8ee50f782f943d994b5f138a2f3804e.png',
        'https://img-blog.csdnimg.cn/bd9458918e224acc81d33936f59faeb7.png',
        'https://img-blog.csdnimg.cn/c6c78e5d137b481e9f1d1d36dd10d0c0.png',
        'https://img-blog.csdnimg.cn/3c71619484dc4cb7a133db869bbd0880.png'
      ]
    };
  },
  beforeMount() {
    this.randomizeCurrentImage();
    this.stage = this.$store.state.stage
    if (this.stage == 1) {
      this.initArr = [1, 2, 3, 4]
      this.selCls = [0, 0, 0, 0]
    }else if (this.stage == 2) {
      this.initArr = [1, 2, 3, 4, 5, 6]
      this.selCls = [0, 0, 0, 0, 0, 0]
    }else if (this.stage == 3) {
      this.initArr = [1, 2, 3, 4, 5, 6, 7, 8, 9]
      this.selCls = [0, 0, 0, 0, 0, 0, 0, 0, 0]
    }else if (this.stage == 4) {
      this.initArr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
      this.selCls = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    }else if (this.stage == 5) {
      this.initArr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
      this.selCls = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    }
    this.num = Math.ceil(Math.random() * 4)
    this.lic = 'lic' + this.stage
    this.thumbnail = require('../assets/images/stage'+this.stage+'/'+this.num+'-1.jpg')
    this.level = require('../assets/images/stage'+this.stage+'/level.png')
    this.$store.commit('SET_NUM', this.num)

    this.initArr.sort(() => {
      return Math.random() - 0.5
    })
    
    // 新增：开始计时器
    this.startTimer();
  },
  computed: {
    imgArr(){
      let arr = []
      this.initArr.map((i) => {
        arr.push({'img':require('../assets/images/stage'+this.stage+'/'+this.num+'-2-'+i+'.jpg'), 'index': i, 'sel': false});
      })
      return arr
    }
  },
  methods: {
    randomizeCurrentImage() {
      const randomIndex = Math.floor(Math.random() * this.images.length);
      this.currentImage = this.images[randomIndex];
    },
    move(val, i, e){
      // 新增：记录步数
      this.moves++;
      
      if (this.selImg.length == 0) {
        this.$set(this.selCls, i, 1)
        this.selImg.push([val, i])
      }else{
        this.$set(this.selCls, this.selImg[0][1], 0)
        this.$set(this.initArr, i, this.selImg[0][0])
        this.$set(this.initArr, this.selImg[0][1], val)
        this.selImg.splice(0, 1)

        // 新增：更新进度
        this.updateProgress();

        const newArr = this.initArr.slice(0)
        const isPass = newArr.every((e, i) => e === i + 1)

        if (isPass) {
          // 新增：停止计时器
          clearInterval(this.timerInterval);
          setTimeout(() => {
            if (this.stage < 5) {
              const nextStage = this.stage + 1
              this.$store.commit('SET_STAGE', nextStage)
              this.$router.replace('/tip')
            }else{
              this.$router.replace('/pass')
            }
          }, 500)
        }
      }
    },
    // 新增方法
    startTimer() {
      this.timerInterval = setInterval(() => {
        this.timer++;
      }, 1000);
    },
    toggleHints() {
      this.showHints = !this.showHints;
    },
    toggleMusic() {
      this.musicPlaying = !this.musicPlaying;
      // 这里可以添加音乐控制逻辑
    },
    shufflePuzzle() {
      this.initArr.sort(() => Math.random() - 0.5);
      this.moves = 0;
      this.updateProgress();
    },
    updateProgress() {
      let correct = 0;
      this.initArr.forEach((val, idx) => {
        if (val === idx + 1) correct++;
      });
      this.progress = (correct / this.initArr.length) * 100;
    }
  },
  // 新增：组件销毁时清除计时器
  beforeDestroy() {
    if (this.timerInterval) {
      clearInterval(this.timerInterval);
    }
  }
};
</script>

<style scoped>
/* 原有样式完全保持不变 */
ul,li{padding: 0;margin: 0;list-style: none}
.bg{
  width: 100%;
  height: 100%;
  position: relative;
  background-color: #023971;
}
.bg img{
  width: 100%;
}
.thumbnail{
  width: 2.734rem;
  position: absolute;
  left: .969rem;
  top: .61rem;
}
.tag{
  width: 4.719rem;
  position: absolute;
  right: .844rem;
  top: 1.456rem;
}
.game{
  width: 8.75rem;
  position: absolute;
  left: .625rem;
  top: 4.451rem;
}
.game li
{
  position: relative;
  overflow: hidden;
  transition: all 0.3s ease; /* 新增：添加过渡效果 */
  cursor: pointer;
}
.game li:hover {
  transform: scale(1.02); /* 新增：悬停放大效果 */
  z-index: 2;
}
.lic1{
  width: 4.375rem;
  height: 4.375rem;
  float: left;
}
.lic2
{
  width: 2.916rem;
  height: 4.375rem;
  float: left;
}
.lic3
{
  width: 2.916rem;
  height: 2.917rem;
  float: left;
}
.lic4
{
  width: 2.916rem;
  height: 2.1875rem;
  float: left;
}
.lic5
{
  width: 2.1875rem;
  height: 2.1875rem;
  float: left;
}
.mask
{
  width: 100%;
  height: 100%;
  background: rgba(0,0,0,.6);
  position: absolute;
  left: 0;
  top: 0;
}

/* 新增样式 */
.game-header {
  position: absolute;
  top: 0.61rem;
  left: 0.969rem;
  right: 0.844rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  z-index: 10;
}

.stats {
  display: flex;
  gap: 1rem;
  background: rgba(255,255,255,0.9);
  padding: 0.5rem 1rem;
  border-radius: 25px;
  box-shadow: 0 4px 15px rgba(0,0,0,0.2);
}

.stat-item {
  display: flex;
  align-items: center;
  gap: 0.3rem;
  font-weight: bold;
  color: #333;
}

.stat-icon {
  font-size: 1.2em;
}

.piece-hint {
  position: absolute;
  top: 5px;
  right: 5px;
  background: #4CAF50;
  color: white;
  border-radius: 50%;
  width: 20px;
  height: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  font-weight: bold;
}

.game-controls {
  position: absolute;
  bottom: 1rem;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  gap: 1rem;
  z-index: 10;
}

.control-btn {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.8rem 1.5rem;
  border: none;
  border-radius: 25px;
  background: linear-gradient(45deg, #ff6b9d, #ffd166);
  color: white;
  font-weight: bold;
  cursor: pointer;
  transition: all 0.3s ease;
  box-shadow: 0 4px 15px rgba(0,0,0,0.2);
}

.control-btn:hover {
  transform: translateY(-3px);
  box-shadow: 0 6px 20px rgba(0,0,0,0.3);
}

.progress-bar {
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 5px;
  background: rgba(255,255,255,0.3);
}

.progress-fill {
  height: 100%;
  background: linear-gradient(90deg, #4CAF50, #8BC34A);
  transition: width 0.5s ease;
  border-radius: 0 5px 5px 0;
}
</style>